livecodefandomcom-20200223-history
Open file
Opens a file so its contents can be accessed or modified. You can use put instead of open file, much simpler. Syntax: open file [ [ text | binary] {update | read | write | append}] Examples: open file "temp.txt" open file "/etc/Glossary" for write open file (myFilePath & "/" & myFileName) for binary read Use the open file command to create a file or prepare an existing file for use by the read from file or write to file commands. However if you use put, you don't need open/read/write. You can optionally specify either text or binary mode. If you specify text mode, when you use the write to file command to put data in the file, any line feed and return characters are translated to the appropriate end-of-line marker for the current operating system before being written to the file. The end-of-line marker on Mac OS and OS X systems is a return character; on Unix, a line feed; on Windows, a CRLF. When you use the read from file command to get data from the file, end-of-line markers are translated to the return constant, and any null characters are translated to spaces (ASCII 32). If you specify binary mode, null characters and end-of-line markers are not translated. If you do not specify a mode, the file is opened in text mode. Use the for write form to open the file for writing. If the file is opened for writing, the write to file command replaces the file's contents from the starting point to the end of the file. Before opening any file for writing, remember to back up the file contents as LiveCode will erase them even if you do not write to the file after opening it. Use the for update form to open the file for both reading and writing. If the file is opened for update, you can use both the read from file and write to file commands to examine or change it, and writing to the file places the data at the specified position without replacing other characters beyond the written data. Use the for append form to open the file for writing. If the file is opened for append, the write to file command adds its data to the end of the file without replacing its current contents. If you do not specify a form, the file is opened for update. If the file is opened in write mode and it doesn't exist, it will be created, but this is not true if the files enclosing folder does not exist. On Mac OS and OS X systems, if the file does not already exist (so that the open file command creates it), the new file's creator signature and file type are set to the values in the fileType property. Any files you have opened are closed automatically when you quit the application. You can use the open file command to open a serial port on Mac OS or Windows systems. On Mac OS systems, specify either "printer:" or "modem:". On Windows systems, specify either "COM1:", "COM2:", or up to "COM9:". Set the serialControlString property before opening the port to specify the baud rate and other settings. On windows, it is imperative not to name a file with one of the reserved device names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8 and LPT9. Also do not use these names followed by an extension, for example, NUL.tx7. If you specify the name of a serial port on Mac OS or Windows systems, LiveCode opens the specified port. The names of serial ports end in a colon (:). Tip: As an alternative to the open file, read from file, and write to file commands, you can also use the URL keyword with get, put, and other commands to access the contents of a file. iOS imposes strict controls over what you can and cannot access. Each application in iOS is stored in its own 'sandbox' folder (referred to as the home folder). An application is free to read and write files within this folder and its descendants, but it is not allowed to access anything outside of the 'sandbox'. An Android application is installed on the phone in the form of its zip package. This means that any assets that are included are not available as discrete files in the native filesystem. In order to manage this the engine essentially 'virtualizes' the asset files you include, allowing (read-only) manipulation with all the standard LiveCode file and folder handling syntax. From 7.0, it's possible to specify an encoding for the file being opened. By doing so, you can straight read or write to a file without having to call textEncode or textDecode; the encoding supported by open file are the same as these text encoding functions. If no encoding is provided, then open file tries to read a Byte Order Mark exists at the beginning of the file. In success, the encoding is adapted and the BOM is ignored. Parameters: *filePath: Specifies the name and location of the file you want to open or create.If you specify a name but not a location, LiveCode assumes the file isin the defaultFolder. If the file you specify doesn't exist, LiveCodecreates it. *encoding: * The result:Use the for read form to open the file for reading. If thefile(keyword) is opened for reading, you can use the read from filecommand to examine its contents, but you cannot modify it. If you usethe for read form and the file does not exist, LiveCode does not createit, and the result function returns"Can't open that file.". Use this form for files on CD-ROM and otherread-only media. See also: constant (command),read from file (command),get (command),close file (command),answer file (command),return (constant),function (control structure),tempName (function),fileType (property),umask (property), Category: file system